package at.pp.match3.core;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import at.pp.match3.game.AbstractEngine;
import at.pp.match3.game.GameEngine;
import at.pp.match3.game.GameThread;
import at.pp.match3.game.render.AbstractRenderer;
import at.pp.match3.game.render.Renderer;

public class Match3Activity extends Activity implements SurfaceHolder.Callback {

    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private GameThread gameThread;
    private AbstractEngine gameEngine;
    private AbstractRenderer renderer;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surfaceView = new SurfaceView(this);
        surfaceView.setFocusable(true);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        setContentView(surfaceView);

        setupGameObjects();
    }

    private void setupGameObjects() {
        renderer = new Renderer();
        gameEngine = new GameEngine(this);
        gameThread = new GameThread();

        gameEngine.setRenderer(renderer);

        gameThread.setEngine(gameEngine);
        gameThread.setSurfaceHolder(surfaceHolder);
        gameThread.setContext(this);
    }

    @Override
    public final void surfaceChanged(final SurfaceHolder holder,
            final int format, final int width, final int height) {
        LOG.log("surfaceChanged");
        gameEngine.setSurfaceSize(width, height);
        // load assets after we know the surface size
        gameEngine.loadAssets();
    }

    @Override
    public final void surfaceCreated(final SurfaceHolder holder) {
        LOG.log("surfaceCreated");
        gameThread.setGameRunning(true);
        gameThread.start();
    }

    @Override
    public final void surfaceDestroyed(final SurfaceHolder holder) {
        boolean retry = true;
        gameThread.setGameRunning(false);
        while (retry) {
            try {
                gameThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

    @Override
    public final boolean onTouchEvent(final MotionEvent event) {
        LOG.log(event.toString());

        if (event.getAction() == MotionEvent.ACTION_DOWN
                || event.getAction() == MotionEvent.ACTION_MOVE) {
            gameEngine.setTouchedAt((int) event.getX(), (int) event.getY());
            gameEngine.setTouched(true);

            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                gameEngine.setMoving(true);
            }
        }

        if (event.getAction() == MotionEvent.ACTION_UP) {
            gameEngine.setTouched(false);
            gameEngine.setMoving(false);
        }

        gameEngine.processMotionEvent();

        try {
            Thread.sleep(30);
        } catch (Exception ex) {

        }

        return true;
    }

}
